package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author: Enid
 * @date: 2025/10/9 16:34
 * @description: 课程管理控制器
 */
@Slf4j
@RequestMapping("/courses")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 课程列表查询
     */
    @GetMapping
    public Result list(CourseDTO courseDTO){
        PageResult<Course> pageResult = courseService.list(courseDTO);
        return Result.success(pageResult);
    }
    /**
     * 新增课程
     */
    @PostMapping
    public Result add(@RequestBody Course course){
        log.info("新增课程：{}",course);
        courseService.addCourse(course);
        return Result.success();
    }
    /**
     * 根据id查询课程
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer id){
        log.info("根据id查询课程id为：{}",id);
        Course course = courseService.selectById(id);
        return Result.success(course);
    }
    /**
     * 修改课程
     */
    @PutMapping
    public Result update(@RequestBody Course course){
        courseService.update(course);
        return Result.success();
    }
    /**
     * 删除课程
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        log.info("删除课程id为：{}",id);
        courseService.deleteById(id);
        return Result.success();
    }
    @GetMapping("/list")
    public Result all(){
        return Result.success(courseService.all());
    }

    @GetMapping("subject/{subject}")
    public Result listBySubject(@PathVariable("subject") String subject){
        List<Course> course = courseService.listBySubject(subject);
        return Result.success(course);
    }

}
